package com.hs.auth.action;
 
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hs.auth.model.HsAccount;
import com.hs.auth.model.HsOrg;
import com.hs.auth.service.IOrgService;
import com.hs.core.action.BaseAction;
import com.hs.core.db.DBMgr;
import com.hs.core.utils.PageParam;
import com.hs.core.utils.PageUtil;
@Controller  
public class OrgController  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(OrgController.class);
	private IOrgService orgService;
	 @Autowired  
	public void setOrgService(IOrgService orgService) {
		this.orgService = orgService;
	}
	 /**
	  * 组织机构信息
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/auth/orgInfo.do", method = RequestMethod.GET)   
     public ModelAndView orgInfo(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         String orgid = request.getParameter("id");//是否存在此参数表明是新增还是修改
          
         if(StringUtils.isNotEmpty(orgid)){
        	 
        	 HsOrg org = (HsOrg)orgService.findByID(HsOrg.class, new Long(orgid));
        	 modelMap.put("obj", org);
         }
         
		 return new ModelAndView("common/auth/orgInfo",modelMap);
		 
	 }
	 /**
	  * 组织机构信息保存
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/auth/saveOrg.do", method = RequestMethod.POST)   
     public ModelAndView saveOrg(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         String orgid = request.getParameter("id");//是否存在此参数表明是新增还是修改
         String orgname = request.getParameter("orgname");
         String pid = request.getParameter("pid");
         String postaddress = request.getParameter("postaddress");
         String postnum = request.getParameter("postnum");
         String province = request.getParameter("province");
         String orgtype = request.getParameter("orgtype");
         String parentunit = request.getParameter("parentunit");
         String orgarea = request.getParameter("orgarea");
         
         String bankname = request.getParameter("bankname");
         String banknum = request.getParameter("banknum");
         String iscontry = request.getParameter("iscontry");
         String iszhongdian = request.getParameter("iszhongdian");
         String isuse = request.getParameter("isuse");
         String orgcode = request.getParameter("orgcode");
         String othername = request.getParameter("othername");
         String memo = request.getParameter("memo");
         HsAccount acc = this.getCurrentuser(request);
         HsOrg org = null;
         if(StringUtils.isNotEmpty(orgid)){
        	 org = (HsOrg)orgService.findByID(HsOrg.class, new Long(orgid));
        	
         }else{
        	 org = new HsOrg();
        	 org.setOrgname(orgname);
         }
         org.setOrgcode(orgcode);
         org.setParentunit(parentunit);
         org.setPid(new Long(pid==null||"".equals(pid)?"0":pid));
         org.setPostaddress(postaddress);
         org.setPostnum(postnum);
         org.setProvince(province);
         org.setOrgtype(new Long(orgtype==null||"".equals(orgtype)?"0":orgtype));
         org.setOrgarea(orgarea);
         org.setBankname(bankname);
         org.setBanknum(banknum);
         org.setIscontry(new Long(iscontry==null||"".equals(iscontry)?"0":iscontry));
         org.setIszhongdian(new Long(iszhongdian==null||"".equals(iszhongdian)?"0":iszhongdian));
         org.setMemo(memo);
         org.setOthername(othername);
         org.setIsuse(new Long(isuse==null||"".equals(isuse)?"0":isuse));
         org.setCreatuser(acc.getId());
         org.setCreatdate(new Date());
         orgService.saveObject(org);
         modelMap.put("obj", org);
         modelMap.put("flag", "fresh");
         List orgList =  orgService.findList("select a  from HsOrg a where pid!=0");
         List roleList = orgService.findList("select a  from HsRole a ");
         modelMap.put("orgList", orgList);
         modelMap.put("roleList", roleList);
         return new ModelAndView("common/auth/orgInfo",modelMap);
		 
	 }
	 /**
	  * 组织机构列表
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/auth/listOrg.do")    
     public ModelAndView listOrg(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		    PageParam pageParam = new  PageParam(request,"auth/listOrg.do");
			StringBuffer buffer = new StringBuffer();
			 
//		    pageParam.getConditionMap().put("orderName", orderName);
//		    pageParam.getConditionMap().put("orderNum", orderNum);
		    
		    //排序结束
			PageUtil pageUtil = null;
			StringBuffer searchBuffer = new StringBuffer(" select a from HsOrg a where a.isuse=1  ");
			String orgname = request.getParameter("orgname");  

			 
			if(StringUtils.isNotBlank(orgname)){
				searchBuffer.append(" and a.orgname like '%").append(orgname).append("%'");
				pageParam.getConditionMap().put("orgname", orgname);
				request.setAttribute("orgname", orgname);
			}  
			 
			// searchBuffer.append(" order by a.prename,a.precomfrom");
			pageParam.setSearchSql(searchBuffer.toString());
			pageUtil = orgService.getOrgList(pageParam);
			modelMap.put("dataList", pageUtil);   
			//List dbsList = dataService.findList("select a  from PdmsDsConfig a ");
	 		//data.put("dbslist", dbsList);
			return new ModelAndView("common/auth/orgList",modelMap);   
		 
	 }
	 /**
	  * 查询单位
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/auth/queryselectunit.do")   
     public ModelAndView queryselectunit(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		   String unitname = request.getParameter("unitname"); 
           DBMgr dbo = this.getDbo(request);	 
		   StringBuffer allfullsql = new StringBuffer("select * from (select orgname,id from hs_org   order by orgname) where rownum<=50");
           if(StringUtils.isNotEmpty(unitname)){
        	   allfullsql = new StringBuffer("select * from (select orgname,id from hs_org where orgname like '%"+unitname+"%' order by orgname) where rownum<=50");
    		   List datalist = dbo.queryList(allfullsql.toString(),null);
    		   modelMap.put("orglist", datalist);
           }else{
    		   List datalist = dbo.queryList(allfullsql.toString(), null);
    		   modelMap.put("orglist", datalist);
           }
		   modelMap.put("unitname", unitname); 
		   return new ModelAndView("common/auth/singleunit",modelMap);
		   
		 
	 } 
	  
	 
	 
}
